package com.zpy.hencoderpractise.hencoder1

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * Created by ZHANG PEIYUAN on 2017/12/13.
 * Email: zhangpeiyuan@hexun.staff.com
 */
class ShapeView : View {

    lateinit var paint: Paint

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attributes: AttributeSet) : super(context, attributes) {
        init()
    }

    constructor(context: Context, attributes: AttributeSet, defStyleAttr: Int) : super(context, attributes, defStyleAttr) {
        init()
    }


    private fun init() {
        paint = Paint()
        paint.color = Color.BLACK
        paint.isAntiAlias = true
        paint.style = Paint.Style.FILL
        paint.strokeWidth = 5f
        paint.strokeCap = Paint.Cap.SQUARE
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        drawCircle(canvas)
//        drawRect(canvas)
//        drawPoint(canvas)
//        drawOval(canvas)
//        drawLine(canvas)
//        drawRoundRect(canvas)
        drawArc(canvas)
    }


    /**
     * 画圆
     */
    private fun drawCircle(canvas: Canvas) {
        canvas.drawCircle(300f, 300f, 200f, paint)
    }

    /**
     * 画方
     */
    private fun drawRect(canvas: Canvas) {
        canvas.drawRect(100f, 100f, 400f, 400f, paint)
    }

    /**
     * 画点
     */
    private fun drawPoint(canvas: Canvas) {
//        canvas.drawPoint(200f, 200f, paint)
        val points = floatArrayOf(0f, 0f, 50f, 50f, 50f, 100f, 100f, 50f, 100f, 100f, 150f, 50f, 150f, 100f)
        canvas.drawPoints(points, 2, 8, paint) //跳过两个数绘制8个数,两两构成一个点的坐标
    }

    /**
     * 画椭圆
     */
    private fun drawOval(canvas: Canvas) {
        canvas.drawOval(RectF(100f, 100f, 600f, 400f), paint)
    }

    /**
     * 画线
     */
    private fun drawLine(canvas: Canvas) {
//        canvas.drawLine(100f, 100f, 300f, 300f, paint)
        val points = floatArrayOf(20f, 20f, 120f, 20f, 70f, 20f, 70f, 120f, 20f, 120f, 120f, 120f, 150f, 20f, 250f, 20f, 150f, 20f, 150f, 120f, 250f, 20f, 250f, 120f, 150f, 120f, 250f, 120f)
        canvas.drawLines(points,paint)
    }

    /**
     * 画圆角矩形
     */
    private fun drawRoundRect(canvas: Canvas) {
        canvas.drawRoundRect(RectF(100f,100f,600f,400f),30f,30f,paint)
    }

    /**
     * 画弧形/扇形
     */
    private fun drawArc(canvas: Canvas) {
        canvas.drawArc(RectF(100f,100f,600f,400f),45f,90f,true,paint)
    }

}